package org.example.cloudcommon.minio.fileReader;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileReader {

    // docx文件进行文本转换
    public StringBuilder DocxFile(String fileUrl) throws IOException {
        /*
        *   File file = new File("path_to_your_docx_file.docx");
            InputStream fis = new FileInputStream(file);
        * */
       //  String fileUrl = "http://192.168.66.99:19000/admintest/李锦堂个人简历.docx";
        HttpURLConnection httpUrl = (HttpURLConnection) new URL(fileUrl).openConnection();
        httpUrl.connect();
        System.out.println("httpUrl"+httpUrl);
        InputStream inputStream = httpUrl.getInputStream();
        XWPFDocument document = new XWPFDocument(inputStream);
        // 2. 提取文本内容
        StringBuilder content = new StringBuilder();
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            content.append(paragraph.getText());
            content.append("\n");
        }
        inputStream.close();
        document.close();
        return content;
    }

    // 读取doc
    public String DocFile() throws Exception {
        String fileUrl = "xxx.docx";
        // 1. 加载 DOC 文档
        File file = new File(fileUrl);
        FileInputStream fis = new FileInputStream(file);
        HWPFDocument document = new HWPFDocument(fis);

        // 2. 提取文本内容
        WordExtractor extractor = new WordExtractor(document);
        String text = extractor.getText();

        // 3. 关闭 DOC 文档和提取器
        extractor.close();
        document.close();
        fis.close();

        // 4. 返回提取的文本内容
        return text;
    }

    //读取 Excel 文件
    public StringBuilder ExcelFile() throws Exception{
        File file = new File("path/to/excel/file");
        FileInputStream inputStream = new FileInputStream(file);
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        StringBuilder stringBuilder = new StringBuilder();
        for (Row row : sheet) {
            for (Cell cell : row) {
                stringBuilder.append(cell.toString());
                stringBuilder.append("\t");
            }
            System.out.println();
        }
        workbook.close();
        return stringBuilder;
    }


    public String EasyExcelReader(){
/*
        String filePath = "path_to_your_excel_file.xlsx";

        // 创建 Excel 读取器
        ExcelReaderBuilder readerBuilder = EasyExcel.read(filePath);

        // 注册读取监听器
        ReadListener<Object> listener = new YourReadListener();
        readerBuilder.registerReadListener(listener);

        // 执行读取操作
        readerBuilder.sheet().doRead();*/

        return null;
    }
}
